using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Xml.Linq;

#pragma warning disable IDE1006 // 命名样式

namespace QNLua
{
	public class SC_Common_Data
	{
		public int scriptId
		{
			get
			{
				return script;
			}
		}
		public int script { get; set; }
		public string? scriptName { get; set; }

		public string? data { get; set; }

	}

	class Program
	{
		static void Main(string[] args)
		{


			string cmdData = """{"script":1,"scriptName":"GameScene","data":"{\"cmd\":40003,\"params\":\"{\\\"option\\\":{\\\"setHandleButton\\\":true,\\\"PathMap\\\":{\\\"level\\\":\\\"武功名内容/修炼等级\\\"}},\\\"type\\\":\\\"SkillTipWindow\\\",\\\"name\\\":\\\"武功详情\\\"}\"}"}""";

			var commonData = JsonConvert.DeserializeObject<SC_Common_Data>(cmdData);
			if (commonData != null)
			{
				string? scriptEventDataStr = commonData.data;
				if (!string.IsNullOrEmpty(scriptEventDataStr))
				{
					var scriptEventData = JsonConvert.DeserializeObject<Dictionary<string, string>>(scriptEventDataStr);
					if (scriptEventData != null && scriptEventData.TryGetValue("params", out string? paramsStr) && paramsStr != null)
					{
						Console.WriteLine("params JSON string: " + paramsStr);

						var luaParams = JsonConvert.DeserializeObject<CommonData.LuaCommonDataParams>(paramsStr);
						if (luaParams != null)
						{
							Console.WriteLine("Deserialized LuaCommonDataParams.type (enum): " + (luaParams.Type?.ToString() ?? "null"));
						}
					}
				}
			}

			// 添加测试用例
			string testJson = """
{
    "type": "SkillTipWindow",
    "rid": "10001",
    "name": "武功详情",
    "option": {
    "SetHandleButton": true,
        "PathMap": {
        "level": "武功名内容/修炼等级"
        },
        "CustomProps": {
        "attack": {
            "ModelName": "攻击力",
            "Path": "属性/攻击",
            "ContentPath": "内容/攻击",
            "Format": "{propName}: {value}",
            "AttDefType": 1,
            "Separator": "\\n"
            }
    },
        "CustomDatas": {
        "extra": {
            "ModelName": "额外属性",
            "Type": "附加",
            "Path": "属性/额外",
            "ContentPath": "内容/额外",
            "Format": "{propName}: {value}"
        }
    }
}
}
""";

			var deserializedParams = JsonConvert.DeserializeObject<CommonData.LuaCommonDataParams>(testJson);
			if (deserializedParams is CommonData.SkillTipWindowParams skillTipParams)
			{
				Console.WriteLine($"SkillTipWindowParams.Type: {skillTipParams.Type}");
				Console.WriteLine("Deserialized SkillTipWindowParams:");
				Console.WriteLine($"rid: {skillTipParams.rid}");
				Console.WriteLine($"name: {skillTipParams.name}");
				Console.WriteLine($"option.SetHandleButton: {skillTipParams.option?.SetHandleButton}");
				Console.WriteLine($"option.PathMap[level]: {skillTipParams.option?.PathMap?["level"]}");
			}
			else
			{
				Console.WriteLine("Failed to deserialize SkillTipWindowParams.");
			}

			string contitionStr = """["T.recharge.type.2.ISOPEN1","==",true, 1]""";
			var condition = JsonConvert.DeserializeObject<Condition.Condition>(contitionStr);
			if (condition != null)
			{
				Console.WriteLine($"condition.left = {condition.LeftValueStr}");
				Console.WriteLine($"condition.op   = {condition.OperatorStr}");
				Console.WriteLine($"condition.right=: {condition.RightValue}");
				Console.WriteLine($"condition.RightIsExpression=: {condition.RightIsExpression}");
			}

			string contitionAndOrStr = """
{
  "1": [
    "itemConfig.type",
    "==",
    9
  ],
  "and_1": {
    "or_1": [
      [
        "itemConfig.type",
        "==",
        0
      ],
      [
        "itemConfig.type",
        "==",
        1
      ]
    ],
    "notAll_3": [
      [
        "itemConfig.smallType",
        "=="
      ],
      [
        "itemConfig.smallType",
        "=="
      ],
      [
        "itemConfig.smallType",
        "=="
      ],
      [
        "itemConfig.smallType",
        "=="
      ]
    ]
  }
}
""";

			var conditionOr = JsonConvert.DeserializeObject<Condition.ConditionOr>(contitionAndOrStr);
			if (conditionOr != null)
			{
				Console.WriteLine($"conditionOr = {conditionOr}");
				// Console.WriteLine($"onditionOr.ConditionOrs.Count   = {conditionOr.ConditionOrs.Count}");
				// Console.WriteLine($"conditionOr.ConditionAnds.Count = {conditionOr.ConditionAnds.Count}");
			}


			string json1 = "{\"action\":1,\"type\":\"example\"}";
			string json2 = "{\"action\":1,\"type\":42}";

			var option1 = JsonConvert.DeserializeObject<CommonData.RedDotOption>(json1);
			var option2 = JsonConvert.DeserializeObject<CommonData.RedDotOption>(json2);

			if (option1 != null)
			{
				Console.WriteLine(option1.Type); // 输出 "example"
			}

			if (option2 != null)
			{
				Console.WriteLine(option2.Type); // 输出 "42"
			}

			string redDotSystemStr = "{\"type\":\"example\",\"depends\":[1,\"dependency\",3],\"Triggers\":[\"trigger1\",2,\"trigger3\"]}";

			var redDotSystemOption = JsonConvert.DeserializeObject<CommonData.RedDotSystemOption>(redDotSystemStr);
			if (redDotSystemOption != null)
			{
				Console.WriteLine("Type: " + redDotSystemOption.Type);  // 输出: example
				Console.WriteLine("Depends: " + string.Join(", ", redDotSystemOption.Depends));  // 输出: 1, dependency, 3
				Console.WriteLine("Triggers: " + string.Join(", ", redDotSystemOption.Triggers));  // 输出: trigger1, 2, trigger3
			}

			Console.WriteLine("\n---------- RedDotParams ---------- ");
			string reddotStr = """{"option":{"action":1,"type":6},"type":"RedDot"}""";
			var commonRedDotParams = JsonConvert.DeserializeObject<CommonData.LuaCommonDataParams>(reddotStr);
			if (commonRedDotParams is CommonData.RedDotParams redDotParams)
			{
				Console.WriteLine($"EventType: {redDotParams.Type}");
				Console.WriteLine($"Type: {redDotParams.option.Type}");
				Console.WriteLine($"Action: {redDotParams.option.Action}");
			}


			Console.WriteLine("\n---------- ItemTipWindow ---------- ");
			string itemTipWindowStr = """{"rid":"r16710019E","option":{"setHandleButton":true},"type":"ItemTipWindow","name":"道具详情"}""";
			var commonItemTipWindowParams = JsonConvert.DeserializeObject<CommonData.LuaCommonDataParams>(itemTipWindowStr);
			if (commonItemTipWindowParams is CommonData.ItemTipWindowParams itemTipWindowParams)
			{
				Console.WriteLine($"EventType: {itemTipWindowParams.Type}");
				Console.WriteLine($"rid: {itemTipWindowParams.rid}");
				Console.WriteLine($"setHandleButton: {itemTipWindowParams.option?.SetHandleButton}");
				Console.WriteLine($"name: {itemTipWindowParams.name}");
			}

			Console.WriteLine("\n---------- ItemTipHandleBtnCfg ---------- ");
			string itemTipButtonCfgStr = """{"type":"ItemTipHandleBtnCfg","config":{"TongYongFangRu":{"name":"放 入","command":"itemtip_tyfr"},"ChuanDai":{"command":"itemtip_chuandai","conditions":{"and_1":{"or_2":[["itemConfig.type"," == ",0],["itemConfig.type"," == ",1],["itemConfig.type"," == ",9]],"notAll_3":[["itemConfig.type"," == ",1],["itemConfig.smallType"," == ",10]],"1":["itemConfig.effectInBag"," == ",0]}},"name":"穿 戴"},"JieSuo":{"command":"itemtip_jiesuo","conditions":{"and_1":[["itemConfig.canLock"," == ",1],["cellData.lockState"," == ",1]]},"name":"解 锁"},"SheZhi":{"command":"itemtip_shezhi","conditions":{"1":["itemConfig.type"," == ",2],"and_2":[["itemConfig.type"," == ",10],["itemConfig.smallType"," == ",18]]},"name":"设 置"},"TongYongXieXia":{"name":"卸 下","command":"itemtip_tyxx"},"ShiYong":{"command":"itemtip_shiyong","conditions":{"not_1":[["itemConfig.type"," == ",0],["itemConfig.type"," == ",1],["itemConfig.type"," == ",9],["itemConfig.type"," == ",3],["itemConfig.type"," == ",5],["itemConfig.type"," == ",6],["itemConfig.type"," == ",7],["itemConfig.type"," == ",8],["itemConfig.type"," == ",11],["itemConfig.type"," == ",12],["itemConfig.type"," == ",13],["itemConfig.type"," == ",17]]},"name":"使 用"},"SuoDing":{"command":"itemtip_suoding","conditions":{"and_1":[["itemConfig.canLock"," == ",1],["cellData.lockState"," == ",0]]},"name":"锁 定"},"DiuQi":{"name":"丢 弃","conditions":[["itemConfig.cantrade"," == ",1]]},"CuiHui":{"command":"itemtip_cuihui","conditions":[["itemConfig.candiscard"," == ",1]],"name":"摧 毁"}}}""";
			var commonItemTipButtonCfgParams = JsonConvert.DeserializeObject<CommonData.LuaCommonDataParams>(itemTipButtonCfgStr);
			if (commonItemTipButtonCfgParams is CommonData.ItemTipHandleBtnCfgParams itemTipHandleBtnCfgParams)
			{
				Console.WriteLine($"EventType: {itemTipHandleBtnCfgParams.Type}");
				Console.WriteLine($"TongYongFangRu.name: {itemTipHandleBtnCfgParams.config["TongYongFangRu"].Name}");
				Console.WriteLine($"TongYongFangRu.name: {itemTipHandleBtnCfgParams.config["TongYongFangRu"].Command}");
				Console.WriteLine($"ChuanDai.name: {itemTipHandleBtnCfgParams.config["ChuanDai"].Name}");
				Console.WriteLine($"ChuanDai.name: {itemTipHandleBtnCfgParams.config["ChuanDai"].Command}");
			}
		}

	}
}

